package main

//验证参数
import (
	"github.com/gin-gonic/gin"
	"net/http"
)

/**
更多信息：https://gopkg.in/go-playground/validator.v8
http://localhost:8080/bind?name=ttt&age=19&address=heelo
*/
//注意开头字母大小，否则取默认值
type Person struct {
	Name    string `form:"name"  binding:"required"`
	Address string `form:"address" binding:"required"`
	Age     int    `form:"age"  binding:"required,gt=18"` //多个用，隔开
}

func main() {
	r := gin.Default()

	r.GET("/bind", paramsStructBind)
	r.POST("/bind", paramsStructBind)
	r.Run()
}

func paramsStructBind(context *gin.Context) {
	var person Person

	if err := context.ShouldBind(&person); err == nil {
		context.String(http.StatusOK, "%v", person)
	} else {
		context.String(http.StatusBadRequest, "validation err %v", err)
		context.Abort()
		return
	}
}
